// pages/add_the_maintenance/add_the_maintenance.js
var dateTimePicker = require('dateTimePicker.js');
let App = getApp();

function jsonSort(jsonObj) {
    let arr = [];
    for (var key in jsonObj) {
        arr.push(key)
    }
    arr.sort();
    let str = '';
    for (var i in arr) {
        str += arr[i] + "=" + jsonObj[arr[i]] + "&"
    }
    return str.substr(0, str.length - 1)
}

Page({

    /**
     * 页面的初始数据
     */
    data: {
        date: '2018-10-01',
        time: '12:00',
        textarea: '', //获取跟进内容
        inp_ths_results: '', //结果 
        inp_sub: '', //后续方向
        tail_metood: 1, //跟进方式
        array: ['电话', '微信', '面对面', '其他'],
        num: '0',
        vip_renewal: ['办卡','升级', '续费', '购课'], //续费
        caller_renewal: [ '续费', '购课'], //续费
        caller_renewal1: ['升级', '购课'], //续费
        num1: '0', //续费index
        dateTimeArray: null, //预约下次联系时间数组
        dateTime: null, //预约下次联系index
        dateTimeArray1: null, //预约到店体验时间
        dateTime1: null, //预约到店体验index
        startYear: 2000,
        starTime: '',
        starDate: '',
        endTime: '',
        endDate: '',
        // plears: '选择时间',
        endYear: 2050,
        type: 2,
        list: [],
        types_str:2,//目的
        disable:true,//不可连续点击的状态
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var This = this;
        console.log(options)
        var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
        var obj1 = dateTimePicker.dateTimePicker1(this.data.startYear, this.data.endYear);
        This.data.date = obj.dateTimeArray[0] + obj.dateTimeArray[1] + obj.dateTimeArray[2];
        this.setData({
            dateTimeArray: obj.dateTimeArray,
            dateTime: obj.dateTime,
            dateTime1: obj1.dateTime1,
            dateTimeArray1: obj1.dateTimeArray1,
            type: options.type,
            id: options.id,
        });
        console.log(This.data.type)
        This.loading();
    },
    loading() {
        var This = this;
        App._get('coach/Maintain/get_member_describe_detail', {
            openid: wx.getStorageSync('openid'),
            phone: wx.getStorageSync('phone'),
            store_id: wx.getStorageSync('store_id'),
            member_id: This.data.id
        }, function(res) {
            if (res.status == 1) {
                This.setData({
                    list: res.data
                })
            } else {
                wx.showToast({
                    title: res.error,
                    image: '../../image/icon_danger.png',
                    duration: 2000
                })
            }
        })
    },
    add_mast(e) {
        var This = this;
        if (This.data.disable == true){
            console.log('aaaa')
            This.setData({
                disable: false
            })
            setTimeout(function(){
                console.log('bbbbbb')
                var time1 = ''
                var time2 = ''
                if (This.data.starDate == '' || This.data.starTime == '') {
                    time1 = ''
                } else {
                    time1 = This.data.starDate + ' ' + This.data.starTime
                }
                if (This.data.endDate == '' || This.data.endTime == '') {
                    time2 = ''
                } else {
                    time2 = This.data.endDate + ' ' + This.data.endTime
                }
                App._get('coach/Maintain/insert_member_describe', {
                    member_id: This.data.id,
                    describe: This.data.textarea, //跟进内容
                    openid: wx.getStorageSync('openid'),
                    phone: wx.getStorageSync('phone'),
                    store_id: wx.getStorageSync('store_id'),
                    result: This.data.inp_ths_results, //结果
                    followUp_orientation: This.data.inp_sub, //后续方向
                    tail_metood: This.data.tail_metood, //跟进方式
                    describe_time: time1, //预约下次联系
                    // describe_time: time2, //预约到店
                    target: This.data.types_str, //目的
                    // type: This.data.type, //1潜客 2会员
                }, function (res) {
                    if (res.status == 1) {
                        wx.showToast({
                            title: '添加成功',
                            duration: 2000
                        })
                        
                        setTimeout(function () {
                            wx.navigateBack({})
                        }, 2000)
                        setTimeout(function(){
                            This.setData({
                                disable: true
                            })
                        },2000)

                    } else {
                        wx.showToast({
                            title: res.error,
                            image: '../../image/icon_danger.png',
                            duration: 2000
                        })
                        This.setData({
                            disable: true
                        })
                    }
                })
            },0) 
        }
        console.log(This.data.caller_renewal[0])
        
    },
    //跟进输入内容
    bindTextAreaBlur: function(e) {
        var This = this;
        console.log(e)
        This.setData({
            textarea: e.detail.value
        })
    },
    //结果输入内容
    ths_results_of: function(e) {
        var This = this;
        console.log(e)
        This.setData({
            inp_ths_results: e.detail.value
        })
    },
    //后续方向
    subsequent: function(e) {
        console.log(e)
        var This = this;
        This.setData({
            inp_sub: e.detail.value
        })
    },
    //跟进方式
    bindPickerChange: function(e) {
        console.log(e)
        var This = this;
        var tail_metood = parseInt(e.detail.value) + 1
        This.setData({
            num: e.detail.value,
            tail_metood: tail_metood
        })
    },
    //目的
    bindrenewalChange: function(e) {
        console.log(e)
        var This = this;
        This.setData({
            num1: parseInt(e.detail.value)
        })
        if (e.detail.value == 0){
            This.setData({
                types_str:3
            })
        } else if (e.detail.value == 1){
            This.setData({
                types_str: 4
            })
        }
    },
    bindrenewalChange1: function (e) {
        console.log(e)
        var This = this;
        This.setData({
            num1: parseInt(e.detail.value)
        })
        if (e.detail.value == 0) {
            This.setData({
                types_str: 2
            })
        } else if (e.detail.value == 1) {
            This.setData({
                types_str: 4
            })
        }
    },

    changeDateTime(e) {
        console.log(e)
        this.setData({
            dateTime: e.detail.value
        });
    },
    changeDateTimeColumn(e) {
        console.log(e)
        var tr = null;
        var gy = null;
        var This = this;
        var arr = this.data.dateTime,
            dateArr = this.data.dateTimeArray;
        arr[e.detail.column] = e.detail.value;
        dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
        This.data.starDate = dateArr[0][arr[0]] + '-' + dateArr[1][arr[1]] + '-' + dateArr[2][arr[2]];
        This.data.starTime = dateArr[3][arr[3]] + ':' + dateArr[4][arr[4]];
        tr = This.data.starDate.split("-");
        gy = This.data.starTime.split(":");
        if (gy[1] == 'undefined') {
            gy[1] = '00';
            This.data.starTime = gy[0] + ":" + gy[1];
        }
        if (tr[1] == '02') {
            if (tr[1] % 400 == 0 || tr[1] % 4 == 0 && year % 100 != 0) {
                if (tr[2] == 'undefined') {
                    tr[2] = 29;
                    This.data.starDate = tr[0] + "-" + tr[1] + "-" + tr[2];
                }
            } else {
                if (tr[2] == 'undefined') {
                    tr[2] = 28;
                    This.data.starDate = tr[0] + "-" + tr[1] + "-" + tr[2];
                }
            }
        }
        this.setData({
            // plears: '',
            starTime: This.data.starTime,
            starDate: This.data.starDate
        });
        console.log(This.data.starTime)
        console.log(This.data.starDate)
    },
    changeDateTime1(e) {
        console.log(e)
        this.setData({
            dateTime1: e.detail.value
        });
    },
    changeDateTimeColumn1(e) {
        var This = this;
        console.log(e)
        var tr = null;
        var gy = null;
        var arr = this.data.dateTime1,
            dateArr = this.data.dateTimeArray1;
        arr[e.detail.column] = e.detail.value;
        dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
        This.data.endDate = dateArr[0][arr[0]] + '-' + dateArr[1][arr[1]] + '-' + dateArr[2][arr[2]];
        This.data.endTime = dateArr[3][arr[3]] + ':' + dateArr[4][arr[4]];
        console.log(This.data.endDate)
        tr = This.data.endDate.split("-");
        gy = This.data.endTime.split(":");

        if (gy[1] == 'undefined') {
            gy[1] = '00';
            This.data.endTime = gy[0] + ":" + gy[1];
        }
        if (tr[1] == '04' || tr[1] == '06' || tr[1] == '08' || tr[1] == '09' || tr[1] == '11') {
            if (tr[2] == 'undefined') {
                tr[2] = 30;
                This.data.endDate = tr[0] + "-" + tr[1] + "-" + tr[2];
            }
        }
        if (tr[1] == '02') {
            if (tr[1] % 400 == 0 || tr[1] % 4 == 0 && year % 100 != 0) {
                if (tr[2] == 'undefined') {
                    tr[2] = 29;
                    This.data.endDate = tr[0] + "-" + tr[1] + "-" + tr[2];
                }
            } else {
                if (tr[2] == 'undefined') {
                    tr[2] = 28;
                    This.data.endDate = tr[0] + "-" + tr[1] + "-" + tr[2];
                }
            }
        }
        this.setData({
            endDate: This.data.endDate,
            endTime: This.data.endTime,
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})